//
//
//		read speech data
//
//

#ifndef _READPCM_H_
#define _READPCM_H_

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <memory.h>
#include <assert.h>
#include <string.h>
#include <io.h>

struct WavHead
{
	//Resource Interchange File Flag (0-3) "RIFF"
	char RIFF[4];
	//File Length ( not include 8 bytes from the beginning ) (4-7)
	long FileLength;
	//WAVE File Flag (8-15) "WAVEfmt "
	char WAVEfmt_[8];
	//Transitory Byte ( normally it is 10H 00H 00H 00H ) (16-19)
	unsigned long noUse;
	//Format Category ( normally it is 1 means PCM-u Law ) (20-21)
	short FormatCategory;
	//NChannels (22-23)
	short NChannels;
	//Sample Rate (24-27)
	long SampleRate;
	//l=NChannels*SampleRate*NBitsPersample/8 (28-31)
	long SampleBytes;
	//i=NChannels*NBitsPersample/8 (32-33)
	short BytesPerSample;
	//NBitsPersample (34-35)
	short NBitsPersample;
	//Data Flag (36-39) "data"
	char data[4];
	//Raw Data File Length (41-43)
	long RawDataFileLength;
};

enum WAVE_FORMAT_TYPE{
	WAVEPCM = 1,
	ALAW = 6,
	MULAW = 7
};

FILE*			GetFileHandle(char *filename, char *mode);
FILE*			OpenAFile(char *fileName, char *openType);
short*			ReadAudio(char *wavFile, int *numSamples, WavHead *header);

unsigned char*	ReadNoHeaderWave(char *wavFile, int *numSamples);
void			WriteNoHeaderWave(char *wavFile, unsigned char *rawData, int numSamples);
unsigned char*	ReadALawNoHeaderWave(char *wavFile, int *numSamples);
void			WriteALawNoHeaderWave(char *wavFile, unsigned char *aulawData, int numSamples, int sampleRate);
short*			ReadAULawPCMWave(char *wavFile, int *numSamples, WavHead *header);
unsigned char*	ReadALawWave(char *wavFile, int *numSamples);
void			WriteALawWave(char *wavFile, unsigned char *aulawData, int numSamples, int sampleRate);

short*	ReadPCM(char *file,int *smpCount);
short*	GetPureSpeech(char *wavFile, int *smpCount);
bool	WaveRewind(FILE *wav_file, WavHead *WavHead);
short*	ReadWave(char *wavFile, int *numSamples);
short*	ReadWave(char *wavFile, int *numSamples, WavHead *header);
void	WriteWave(char *wavFile, short *waveData, int numSamples, int sampleRate, WAVE_FORMAT_TYPE type);
void	FillWaveHeader(void *buffer, int raw_wave_len, int sampleRate, WAVE_FORMAT_TYPE type);
short 	SwapInt16(short data);
short*  ReadALawNoHeaderShortWave(char *wavFile, int *numSamples, WavHead *wavHead);

#endif //_READPCM_H_
